package com.thx.log.dao;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.common.util.AppContext;
import com.thx.common.util.DateUtil;
import com.thx.log.LogUtil;
import com.thx.log.model.Log;

@Repository
public class LogDao extends PaginationDao<Log> {
	
	public List<Log> searchLogs(String userName, String startTime, String endTime) {
		String hql = "from Log where comId = ?";
		List<Object> objs = new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isNotBlank(userName)) {
			hql += " and userName like ?";
			objs.add("%"+StringUtils.strip(userName)+"%");
		}
		hql = handleTimeParam(startTime, endTime, hql, objs); 
		hql += " order by createDate desc";
		return findPageList(hql, objs.toArray());
	}

	public Log findLastLoginLogByUserId(String module, String action, String userId) {
		if(StringUtils.isBlank(module) || StringUtils.isBlank(action) || StringUtils.isBlank(userId))
			return null;
		String hql = "from Log where module = ? and action = ? and userId = ? order by createDate desc";
		List<Log> logs = createQuery(hql, module, action, userId).list();
		if(logs != null && logs.size() > 0){
			return logs.get(0);
		}
		return null;
	}
	
	public Long getAppAccessNum(String appName, String startTime, String endTime) {
		String hql = "select count(*) from Log where action = '"+LogUtil.ACTION_LOGIN+"'";
		List<Object> objs =new ArrayList<Object>();
		if (StringUtils.isNotBlank(appName)) {
			hql += " and appName = ?";
			objs.add(StringUtils.strip(appName));
		}
		hql = handleTimeParam(startTime, endTime, hql, objs);  
		return (Long) createQuery(hql,objs.toArray()).uniqueResult();
	}
	
	public List<String> getAppModule(String appName) {
		String hql = "select distinct module from Log where 1=1";
		List<Object> objs = new ArrayList<Object>();
		if (StringUtils.isNotBlank(appName)) {
			hql += " and appName = ?";
			objs.add(StringUtils.strip(appName));
		}
		hql += " order by createDate desc";
		return createQuery(hql,objs.toArray()).list();
	}
	
	public Long getModuleAccessNum(String appName, String module, String startTime, String endTime) {
		String hql = "select count(*) from Log where 1=1";
		List<Object> objs =new ArrayList<Object>();
		if (StringUtils.isNotBlank(appName)) {
			hql += " and appName = ?";
			objs.add(StringUtils.strip(appName));
		}
		if (StringUtils.isNotBlank(module)) {
			hql += " and module = ?";
			objs.add(StringUtils.strip(module));
		}
		hql = handleTimeParam(startTime, endTime, hql, objs); 
		return (Long) createQuery(hql,objs.toArray()).uniqueResult();
	}
	
	private String handleTimeParam(String startTime, String endTime, String hql, List<Object> objs) {
		try {
			if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)) {
				Date start = DateUtil.parse(StringUtils.strip(startTime), "yyyy-MM-dd HH:mm:ss");
				hql +=" and createDate > ?";
				objs.add(start);
			} 
			if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
				Date start = DateUtil.parse(StringUtils.strip(startTime), "yyyy-MM-dd HH:mm:ss");
				Date end = DateUtil.parse(StringUtils.strip(endTime), "yyyy-MM-dd HH:mm:ss");
				hql +=" and createDate between ? and ?";
				objs.add(start);
				objs.add(end);
			} 
			if(StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)){
				Date end = DateUtil.parse(StringUtils.strip(endTime), "yyyy-MM-dd HH:mm:ss");
				hql +=" and createDate < ?";
				objs.add(end);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hql;
	}

}
